﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Fysh.ComputerScience.Genetics
{
    public class SequenceAligner
    {
        private Thread _asyncAlignThread;
        private AlignmentResult[] _currentResult;
        private AutoResetEvent[] _asyncAutoResetEvent;
        private ManualResetEvent[] _endAsyncManualResetEvent;

        private AsyncAlignmentResult _asyncAlignmentResult;

        private bool _asyncIsCompleted;
        private bool _asyncCompletedSynchronously;

        /// <summary>
        /// Discovers the maximum scoring alignment for two or more sequences using the Smith-Waterman algorithm.
        /// </summary>
        /// <param name="sequences">Collection of sequences to be aligned</param>
        /// <param name="dnaModel">Evolutionary DNA model used to determine distances between gene transitions and transvertions</param>
        /// <param name="dnaModelParameterArray">Array of parameters used to control the DNA model selected</param>
        /// <param name="executionMode">Execution Mode used</param>
        /// <returns>An SequenceResult that contains information about the completed alignment.</returns>
        public AlignmentResult Align(SequenceCollection sequences, EvolutionaryDnaModel dnaModel, double[] dnaModelParameterArray, ExecutionMode executionMode)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Asynchronous version of the Align method.
        /// </summary>
        /// <param name="sequences">Collection of sequences to be aligned</param>
        /// <param name="dnaModel">Evolutionary DNA model used to determine distances between gene transitions and transvertions</param>
        /// <param name="dnaModelParameterArray">Array of parameters used to control the DNA model selected</param>
        /// <param name="callback">Method to be called upon completion of the alignment</param>
        /// <returns>An System.IAsyncResult that contains information about the status of the alignment.</returns>
        public IAsyncResult BeginAlign(SequenceCollection sequences, EvolutionaryDnaModel dnaModel, double[] dnaModelParameterArray, Fysh.ComputerScience.Genetics.ExecutionMode executionMode, AsyncCallback callback)
        {
            //create and start the align thread
            _asyncAlignThread = new Thread(new ThreadStart(delegate() { this.Align(sequences, dnaModel, dnaModelParameterArray, executionMode); }));
            _asyncAlignThread.Start();

            return _asyncAlignmentResult;
        }

        /// <summary>
        /// Ends an asynchronous alignnent.
        /// </summary>
        /// <param name="asyncResult">An System.IAsyncResult that contains information about the status of the alignment.</param>
        /// <returns>An SequenceResult that contains information about the completed alignment.</returns>
        public AlignmentResult EndAlign(AsyncAlignmentResult asyncResult)
        {
            throw new System.NotImplementedException();

            //block calling thread until the asynchronous operation is completed
            _endAsyncManualResetEvent[1].WaitOne();
        }
    }
}
